// component/pay/index.js
const API = require('../../common/constant.js');
const util = require('../../utils/util');
Component({
  behaviors: [],
  properties: {
    payShow: {
      type: Boolean,
      value:false,
    },
    payWay:{
      type: String,
      value:'',
    },
    realPayPrice: {
      type: Number,
      value: 0,
    },
  },
  data: {
    // 私有数据，可用于模版渲染
    wayList: [
      { value: 'wx', select: false },
      { value: 'balance', select: false }
    ],
    balance: {
      type: Number,
      value: 0,
    },
  },
  attached(){
    console.log('pay_component');
    this.getBalance();
  },
  methods: {
    chooseWay(e){
      let index = e.currentTarget.dataset.index;
      let select = e.currentTarget.dataset.select;
      let wayList = this.data.wayList;
      wayList.forEach((item)=>{item.select = false;})
      wayList[index].select = true;
      this.data.payWay = wayList[index].value;
      this.setData({wayList: wayList})
    },
    cancelPay(){
      this.setData({ payShow: false })
    },
    triggerPay(){
      if (!this.data.payWay) {
        wx.showToast({ title:'请选择支付方式', image: '../../images/warn.png', duration: 1000 });
        return false;
      }
      this.setData({ payShow: false });
      this.triggerEvent('payEvent', this.data.payWay);
    },
    getBalance() {
      let url = API.URL.WX.FINDUSERINFO;
      util.http({
        url: url,
        success: (res) => {
          console.log(res);
          if (res.status == 200) {
            this.setData({
              balance: res.data.bussData.totalBalance
            })
          }
        },
        fail: (res) => {
          wx.showToast({ title: res.msg, image: '../../images/warn.png', duration: 1000 });
        },
        revertBack: () => {
          this.getInfo();
        }
      })
    },
  }
})